<template>
  <div class="draW-sty">
    <div class="info">
      <div class="left">
        <div>
          <div class="main-swiper" id="main-swiper">
            <div v-show="!currentImage" class="swiper-wrapper">
              <div class="swiper-slide" v-for="(item, index) in dataInfo.imageList" :key="index">
                <img class="banner" :src="item" alt="">
              </div>
            </div>
            <div v-show="currentImage" class="current-goods">
              <img :src="currentImage" alt="">
            </div>
          </div>
          <div id="thumbs" class="thumbs-swiper">
            <div class="swiper-wrapper">
              <div class="swiper-slide" @click="currentImage = ''" v-for="(item, index) in dataInfo.imageList"
                :key="index * 10">
                <img class="banner" :src="item" alt="">
              </div>
            </div>
          </div>
        </div>
        <div class="share">
          <div></div>
          <Button v-if="false" class="search-btn" type="primary">找相似</Button>
          <div class="share-right">
            <div class="btn" @click="shareBtn">
              <span class="iconfont icon-fenxiang"></span>
              {{ $t('goods.share_title') }}
            </div>
            <div class="btn" @click="onCollect">
              <span v-if="dataInfo.favorite" class="iconfont icon-31shoucangxuanzhong"></span>
              <span v-else class="iconfont icon-31shoucang"></span>
              {{ $t('goods.favorite_it') }}
            </div>
          </div>
        </div>
      </div>

      <div class="right" v-if="dataInfo">
        <black-good-rig :info="dataInfo" :isSpuLock="isSpuLock" @on-change="onGoodsImage" @refreshData="updateMet"
          @copyLink="copyGoodMet" @addSuccess="addSuccess"></black-good-rig>
      </div>
    </div>
    <div class="intro">
      <div v-html="dataInfo.description"></div>
    </div>

    <!-- 推广分享弹框 -->
    <Modal v-model="sharePop" :mask-closable="true" :styles="{ top: '300px' }" :lock-scroll="true" width="450">
      <div slot="header" class="share-title">{{ $t('goods.share_title') }}</div>
      <div class="share-link">{{ shareLink }}</div>
      <div class="share-desc">{{ $t('goods.share_desc') }}</div>
      <div slot="footer">
        <Button type="error" size="large" style="background-color: #FF186B;" @click="copyLink">{{ $t('goods.copyLink')
          }}</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import Swiper from "swiper";
import "swiper/css/swiper.css";
import { fromSeoFriendsChannel, toSeoFriendsChannel } from "@/common/common";
import { queryProductDetail, getShareLink } from "@/api/product";
import { favoritesAdd, favoritesDeleteBatch } from "@/api/favorites";
import blackGoodRig from "./black-good-rig.vue";
export default ({
  props: {
    dataInfo: Object,
    isSpuLock: Number
  },
  components: { blackGoodRig },
  data() {
    return {
      currentImage: '',
      sharePop: false,
      shareLink: '',
      createForm: {
        type: 'GOODS',
      },
    }
  },
  mounted() {
    this.onInit();
  },
  methods: {
    addSuccess() {
      this.$emit('addCartStatus')
    },
    onInit() {
      this.currentSwiper = new Swiper("#thumbs", {
        observer: true, // 修改swiper自己或子元素时，自动初始化swiper
        observeParents: true, // 修改swiper的父元素时，自动初始化swiper
        spaceBetween: 10,
        slidesPerView: 4,
        freeMode: true,
        watchSlidesProgress: true,
      });
      let swiper2 = new Swiper("#main-swiper", {
        observer: true, // 修改swiper自己或子元素时，自动初始化swiper
        observeParents: true, // 修改swiper的父元素时，自动初始化swiper
        spaceBetween: 10,
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
        thumbs: {
          swiper: this.currentSwiper,
        }
      });
    },
    onGoodsImage(info) {
      this.currentImage = info.image;
    },
    shareBtn() {
      this.createForm.jsonDetail = JSON.stringify({ channel: toSeoFriendsChannel(this.dataInfo.channel), id: this.dataInfo.spuNo });
      this.sharePop = true;
      getShareLink(this.createForm).then((res) => {
        this.shareLink = res.result;
      })
    },
    copyLink() {
      navigator.clipboard.writeText(this.shareLink).then(() => {
        this.$Message.success(this.$t("public.copySuccess"));
      }).catch(err => {
        this.$Message.error(this.$t("public.copyFail"));
      });
    },
    onCollect() {
      if (this.dataInfo.favorite) {
        this.deleteFavorites();
      } else {
        this.addFavorites();
      }
    },
    deleteFavorites() {
      // 删除收藏
      this.dataInfo.favorite = false;
      favoritesDeleteBatch([this.dataInfo.spuNo], 'spu');
    },
    addFavorites() {
      // 收藏商品
      this.dataInfo.favorite = true;
      // spu: 商品,shop店铺
      favoritesAdd(this.dataInfo.channel, this.dataInfo.spuNo, 'spu');
    },
    copyGoodMet() {
      const domain = window.location.origin;
      const queryParams = `?id=${this.dataInfo.spuNo}&channel=${this.dataInfo.channel}`;
      const copyUrl = `${domain}/goods/details${queryParams}`;
      this.$copyText(copyUrl).then(
        () => {
          this.$Message.success(this.$t("public.copySuccess"));
        },
        () => {
          this.$Message.error(this.$t("public.copyFail"));
        }
      );
    },
    updateMet(val) {
      this.$emit('updateMet', val)
    }
  }
})
</script>
<style lang="less" scoped>
.draW-sty {
  margin: 0 120px;

  .info {
    margin-top: 40px;
    display: flex;
    flex-shrink: 0;

    .left {
      flex: 0 0 420px;
      width: 420px;
      overflow: hidden;

      .main-swiper {
        width: 100%;
        height: 420px;
        overflow: hidden;
        border-radius: 2px;

        .swiper-slide {
          display: flex;
          align-items: center;
          justify-content: center;

          img {
            display: block;
            width: 100%;
          }
        }

        .current-goods {
          img {
            display: block;
            width: 100%;
            border: none;
            border-radius: 11px;
          }
        }
      }

      .thumbs-swiper {
        height: 20%;
        box-sizing: border-box;
        padding: 10px 0;

        .swiper-slide {
          display: flex;
          align-items: center;
          justify-content: center;
          height: 100%;
          opacity: 1;
          border: 1px solid @t-table-border-color;
          border-radius: 2px;

          img {
            display: block;
            width: 100%;
            cursor: pointer;
            height: 100px;
            object-fit: cover;
          }
        }

        .swiper-slide-thumb-active {
          opacity: 0.6;
        }

      }
    }

    .share {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 26px;
      padding-bottom: 16px;
      border-bottom: 1px solid #ECECEC;

      .search-btn {
        width: 90px;
      }

      .share-right {
        display: flex;
        align-items: center;

        .btn {
          margin-left: 24px;
          cursor: pointer;
          color: #FF186B;
          font-size: 19px;
          font-weight: 400;
          line-height: 22px;
          font-family: PingFang HK, PingFang HK;
        }
      }
    }

    .right {
      flex: 1;
      padding-left: 42px;
    }
  }

  .intro {
    width: 790px;
    margin: 0 auto;
    margin-top: 80px;
  }
}

/deep/.ivu-modal-header {
  border: none;
}

/deep/.ivu-modal-footer {
  border: none;
}

.share-title {
  font-size: 18px;
  font-weight: bold;
  margin-top: 12px;
}

.share-link {
  font-size: 18px;
  font-weight: bold;
}

.share-desc {
  font-size: 16px;
  font-weight: 500;
  margin-top: 4px;
}
</style>
